package com.cxyd.modules.wcs.controller;

import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.wcs.dto.PostTcpDTO;
import com.cxyd.modules.wcs.service.PostTcpService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 下发指令
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-11-04
 */
@RestController
@RequestMapping("wcs/post-tcp")
public class PostTcpController {
    private final PostTcpService PostTcpService;

    public PostTcpController(PostTcpService PostTcpService) {
        this.PostTcpService = PostTcpService;
    }

    @GetMapping("page")
    @RequiresPermissions("wcs:post-tcp:page")
    public Result<PageData<PostTcpDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<PostTcpDTO> page = PostTcpService.page(params);

        return new Result<PageData<PostTcpDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wcs:post-tcp:info")
    public Result<PostTcpDTO> get(@PathVariable("id") Long id) {
        PostTcpDTO data = PostTcpService.get(id);

        return new Result<PostTcpDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wcs:post-tcp:save")
    public Result save(@RequestBody PostTcpDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        PostTcpService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wcs:post-tcp:update")
    public Result update(@RequestBody PostTcpDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        PostTcpService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wcs:post-tcp:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        PostTcpService.delete(ids);

        return new Result();
    }
}